//
//  AbstractMediator.swift
//  TertiumQuidPattern
//
//  Created by mac on 2017/11/2.
//  Copyright © 2017年 RRC. All rights reserved.
//

import Foundation
//中介抽象类
public class AbstractMediator{
    var purchase:Purchase
    var sale:Sale
    var stock:Stock
    
    //构造函数
    init() {
        purchase = Purchase(mediator:mediator)
        sale = Sale(mediator:mediator)
        stock = Stock(mediator:mediator)
    }
    
    func execute(str:String, objects:AnyObject...){}
    
}

//具体中介者
public class Mediator:AbstractMediator{
    ///中介者最重要的方法
    override func execute(str: String, objects:AnyObject...) {
        
        if str == "purchase.buy" {//采购电脑
            self.buyComputer(number: objects[0] as! Int)
        }else if str == "sale.sell" {//销售电脑
            self.sellComputer(number: objects[0] as! Int)
        }else if str == "sale.offsell" {//折价销售
            
            self.offSell()
        }else if str == "stock.clear" {//清理
            self.clearStock()
        }
    }
    
    ///采购电脑
    func buyComputer(number:Int){
        let saleStatus = super.sale.getSaleStatus()
        if saleStatus > 80 {//销售情况良好
            print("采购IBM电脑\(number)台")
            super.stock.increase(number: number)
        }else{//销售情况不好
            let buyNumber = number/2
            print("采购IBM电脑：\(buyNumber)台")
        }
    }
    
    ///销售电脑
    func sellComputer(number:Int){
        if super.stock.getStockNumber() < number {
            //库存数量不够销售
            super.purchase.buyIBMcomputer(number: number)
        }
        super.stock.decrease(number: number)
    }
    
    //折价销售电脑
    func offSell(){
        print("折价销售IBM电脑\(stock.getStockNumber())台")
    }
    
    ///清仓处理
    func clearStock(){
        //要求清仓销售
        super.sale.offSale()
        //要求采购人员不要采购
        super.purchase.refuseBuyIBM()
    }
    
}
